<template>
	<view class="content">
		<!-- 收货地址 开始 -->
		<view class="address" @click="goChoseAddress">
			<view class="left display align-items" v-if="addressListdefult==1">
				<image class="left_icon" src="../../../static/icon/address.png" mode=""></image>
				<view class="tit">
					<view class="name">
						{{addressList.consignee}} {{addressList.mobile}}
					</view>
					<view class="add">
						{{addressList.province_name}}-{{addressList.city_name}}-{{addressList.county_name}}-{{addressList.address}}
					</view>
				</view>
			</view>
			<view class="left display align-items" v-if="addressListdefult==0">
				<view class="nothing_address">+新增地址</view>
			</view>
			<image class="fh" src="../../../static/icon/fh.png" mode=""></image>
		</view>
		<!-- 收货地址 结束 -->
		<!-- 商品信息 开始 -->
		<view class="shop_message">
			<view class="all">
				<view class="title">
					商品信息
				</view>
				<view class="list" v-for="(item,index) in orderList.goods" :key="index">
					<image :src="imgUrl+item.goods_image" mode=""></image>
					<view class="right">
						<view class="name">{{item.goods_name}}</view>
						<view class="num">
							×{{item.goods_num}}
						</view>
						<view class="mony">
							¥{{item.goods_price}}
						</view>
					</view>
				</view>
				<view class="allMony border_bottom">
					<view class="left">
						商品总额
					</view>
					<view class="right">
						¥{{orderList.goods_amount}}
					</view>
				</view>
				<view class="allMony">
					<view class="left">
						物流运费
					</view>
					<view class="right_pay">
						{{orderList.postage}}
					</view>
				</view>
			</view>
		</view>
		<!-- 商品信息 结束 -->
		<!-- 支付方式 开始 -->
		<view class="payment" v-if="orderList.pay_time==null">
			<view class="all border_bottom  display align-items space-between" v-if="wxpayOpen!=0">
				<view class="display align-items ">
					<image class="allMony_icon" src="../../../static/icon/wx_pay.png" mode=""></image>
					微信支付
				</view>
				<view class="chose_icon" :class="{ actPay: payId===1}" @click="chosePay(1)">
				</view>
			</view>
			<view class="all  display align-items space-between">
				<view class="display align-items">
					<image class="allMony_icon" src="../../../static/icon/ye_pay.png" mode=""></image>
					余额支付
				</view>
				<view class="chose_icon" :class="{actPay : payId===2}" @click="chosePay(2)">
				</view>
			</view>
		</view>
		<!-- 微信支付 -->
		<view v-else>
			<view class="payment" v-if="orderList.pay_type=='wxpay'">
				<view class="all border_bottom  display align-items space-between">
					<view class="display align-items ">
						<image class="allMony_icon" src="../../../static/icon/wx_pay.png" mode=""></image>
						微信支付
					</view>
					<view class="chose_icon" :class="{ actPay: payId===1}">
					</view>
				</view>
			</view>
			<!-- 余额支付 -->
			<view class="payment" v-if="orderList.pay_type=='amount'">
				<view class="all  display align-items space-between">
					<view class="display align-items">
						<image class="allMony_icon" src="../../../static/icon/ye_pay.png" mode=""></image>
						余额支付
					</view>
					<view class="chose_icon" :class="{actPay : payId===2}">
					</view>
				</view>
			</view>
		</view>

		<!-- 支付方式 结束 -->
		<!-- 复购积分 开始 -->
		<view class="integral">
			<view class="title">
				复购积分可抵用{{score}}元
			</view>
			<switch :disabled="switchBtn" color="#4b968d" style="transform: scale(0.8,0.8);margin-right: -15rpx;" :checked="switchCheck"
			 @change="switch1Change" />
		</view>
		<!-- 复购积分 结束 -->
		<!-- 立即支付 开始 -->
		<view class="dw">
			<text class="mony">合计<text>:￥{{orderList.order_amount}}</text></text>
			<text class="zhifu" @click="ConfirmOrderbtn">立即支付</text>
		</view>
		<view class="ye_money" v-if="payId==2">
			余额：{{amount}}
		</view>
		<!-- 立即支付 结束 -->
		<uni-popup ref="popup" type="center">
			<view class="popup_container ">
				<view class="popup_top back">
					<view class="popup_top_name letter-spacing">¥{{popupMoney.order_amount}}</view>
					<view class="popup_top_info auto  ">
						<view class="popup_top_info_contant display space-between border_bottom">
							<view class="popup_top_info_contant_ls">微信支付</view>
							<view class="popup_top_info_contant_rg">¥{{popupMoney.wxpay}}</view>
						</view>
						<view class="popup_top_info_contant display space-between border_bottom">
							<view class="popup_top_info_contant_ls">余额支付</view>
							<view class="popup_top_info_contant_rg">¥{{popupMoney.amount}}</view>
						</view>
						<view class="popup_top_info_contant display space-between border_bottom">
							<view class="popup_top_info_contant_ls">复购积分</view>
							<view class="popup_top_info_contant_rg">¥{{popupMoney.score}}</view>
						</view>
					</view>
					<view class="popup_button letter-spacing auto" @click="payWx()">确定</view>
				</view>
				<image class="popup_close auto" @click="closePopup" src="../../../static/icon/close.png" mode=""></image>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		uniIcons,
		uniPopup
	} from '@dcloudio/uni-ui'
	export default {
		components: {
			uniIcons,
			uniPopup,
		},
		data() {
			return {
				goods_id: '',
				goods_num: '',
				goods_ids: '',
				type: '',
				imgUrl: '',
				orderList: [], //订单数据
				score: '', //积份
				amount: 0, //余额
				addressListdefult: 2,
				addressList: {},
				payId: 1,
				payintegral: 1,
				popupMoney: {},
				orderId: '', //订单id
				switchBtn: false,
				switchCheck: true,
				repay: 0,
				wxpayOpen: ''
			}
		},
		onLoad(option) {
			this.imgUrl = this.$imgUrl
			if (!uni.getStorageSync('openid')) {
				return uni.showToast({
					title: '请先登录',
					icon: "none"
				})
			} else {
				this.orderId = option.orderId
				this.getConfirmOrder()
			}
		},
		onShow() {
			var aid = 0
			if (uni.getStorageSync('addressId') != 0) {
				this.getAddressEdit(uni.getStorageSync('addressId'))
				uni.setStorageSync('addressId', aid)
			} else {
				this.getConfirmOrder()
			}
		},
		methods: {
			// 關閉支付彈窗
			closePopup() {
				this.$refs.popup.close();
			},
			// 支付
			ConfirmOrderbtn() {
				this.seeMoney()
			},
			// 订单金额支付查看
			seeMoney() {
				let that = this
				var data = {
					openid: uni.getStorageSync('openid'),
					id: that.orderList.id,
					method: that.payId,
					use_score: that.payintegral,
					repay: that.repay,
					address_id: that.addressList.id
				}
				if (that.addressList.id) {
					this.$myRequest.post(this.$api.order.pay, data).then(res => {
						if (res.code == '200') {
							this.$refs.popup.open();
							this.popupMoney = res.data
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none"
							})
						}
					})
				} else {
					uni.showToast({
						title: '请选择您的地址',
						icon: "none"
					})
				}
			},
			// 是否使用积分
			switch1Change: function(e) {
				var value = e.target.value
				if (!value) {
					this.payintegral = 0
				} else {
					this.payintegral = 1
				}
			},
			// 选择支付方式
			chosePay(id) {
				this.payId = id
			},
			// 获取默认地址
			getDefaultAddress() {
				let that = this
				var data = {
					openid: uni.getStorageSync('openid')
				}
				this.$myRequest.post(this.$api.address.getDefaultAddressUrl, data).then(res => {
					if (res.code == '200') {
						var address = res.data.data
						if (!address) {
							that.addressListdefult = 0
						} else {
							that.addressList = address
							that.addressListdefult = 1
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			// 获取商品订单
			getConfirmOrder() {
				let that = this
				var data = {
					id: that.orderId,
					openid: uni.getStorageSync('openid')
				}
				this.$myRequest.post(this.$api.order.getOrderForPay, data).then(res => {
					if (res.code == '200') {
						that.orderList = res.data.order
						that.amount = res.data.amount
						that.score = res.data.score

						var aid = 0
						if (uni.getStorageSync('addressId') != 0) {
							this.getAddressEdit(uni.getStorageSync('addressId'))
							uni.setStorageSync('addressId', aid)
						} else {
							if (!res.data.order.address_id) {
								this.getDefaultAddress()
							} else {
								var addressList = {
									consignee: res.data.order.consignee,
									mobile: res.data.order.mobile,
									province_name: res.data.order.province_name,
									city_name: res.data.order.city_name,
									county_name: res.data.order.county_name,
									address: res.data.order.address,
									id:res.data.order.address_id,
								}
								that.addressListdefult = 1
								that.addressList = addressList
							}
						}

						if (res.data.order.pay_time !== null) {
							this.repay = 1
							if (res.data.order.pay_type == 'amount') {
								this.payId = 2
								this.switchBtn = true
							}
							if (res.data.order.pay_type == 'wxpay') {
								this.payId = 1
								this.switchBtn = true
							}
							if (res.data.order.score > 0) {
								// 开启状态
								this.switchCheck = true
							} else {
								// 关闭
								this.switchCheck = false
							}
						} else {
							console.log(res.data.order.wxpay_open)
							this.wxpayOpen = res.data.wxpay_open
							this.payId = 2
							this.repay = 0
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			// 选择地址
			goChoseAddress() {
				uni.navigateTo({
					url: "../../mine/address/checkAddress"
				})
			},
			// 获取地址详情
			getAddressEdit(id) {
				let that = this
				var data = {
					openid: uni.getStorageSync('openid'),
					id: id
				}
				this.$myRequest.post(this.$api.address.getAddressUrl, data).then(res => {
					if (res.code == '200') {
						var address = res.data.data
						that.addressList = address
						that.addressListdefult = 1
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			// 微信支付
			payWx() {
				let that = this
				var data = {
					id: that.orderId,
					openid: uni.getStorageSync('openid'),
				}
				that.$refs.popup.close();
				this.$myRequest.post(this.$api.order.confirm, data).then(res => {
					if (res.code == '200') {
						var info = res.data.data
						if (!info) {
							uni.showToast({
								title: "支付成功",
								duration: 3000,
								icon: 'success',
								success() {
									setTimeout(function() {
										var status = 2
										uni.redirectTo({
											url: '../../mine/orderForm/index?status=' + status
										});
									}, 1500);
								}
							});
						} else {
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: info.timeStamp + '',
								nonceStr: info.nonceStr,
								package: info.package,
								signType: info.signType,
								paySign: info.paySign,
								success: function(res) {
									uni.showToast({
										title: "支付成功",
										duration: 3000,
										icon: 'success',
										success() {
											setTimeout(function() {
												var status = 2
												uni.redirectTo({
													url: '../../mine/orderForm/index?status=' + status
												});
											}, 1500);
										}
									});
								},
								fail: function(err) {
									uni.showToast({
										title: '支付取消',
										icon: "none",
										success() {
											setTimeout(function() {
												var status = 1
												uni.redirectTo({
													url: '../../mine/orderForm/index?status=' + status
												});
											}, 1500);
										}
									})
									console.log('fail:' + JSON.stringify(err));
								}
							});
						}

					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})


			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.address {
			// height: 130rpx;
			border-bottom: 5rpx dashed #7F99BB;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-left: 30rpx;
			padding-right: 30rpx;
			color: #666666;
			padding-top: 20rpx;
			padding-bottom: 20rpx;

			.name {
				font-size: 26rpx;
				color: #333333;
			}

			.add {
				font-size: 26rpx;
				margin-top: 8rpx;
			}

			.left {
				display: flex;

				.tit {
					width: 626rpx;
				}

				.left_icon {
					width: 25rpx;
					height: 30rpx;
					margin-right: 15rpx;
				}
			}

			.fh {
				width: 15rpx;
				height: 27rpx;
			}

			.nothing_address {
				// border: 2px solid #7F99BB;
				font-size: 30rpx;
				padding: 0 20rpx;
				height: 70rpx;
				line-height: 70rpx;
				text-align: center;
				border-radius: 8rpx;
			}
		}

		.shop_message {
			border-bottom: 10rpx solid #EFF0F2;

			.all {
				width: 690rpx;
				margin: 0 auto;

				.title {
					padding-left: 12rpx;
					line-height: 27rpx;
					margin: 30rpx 0;
					font-size: 28rpx;
					color: #333333;
					border-left: 5rpx solid #4b968d;
				}

				.list {
					width: 100%;
					margin-bottom: 20rpx;
					height: 200rpx;
					background: #FFFFFF;
					border: 1px solid #E3E3EF;
					border-radius: 10rpx;
					display: flex;
					align-items: center;

					image {
						width: 156rpx;
						height: 156rpx;
						margin: 0 22rpx;
					}

					.right {
						width: 475rpx;

						.name {
							font-size: 25rpx;
							color: #333333;
							height: 65rpx;
						}

						.num {
							text-align: right;
							font-size: 26rpx;
							color: #666666;
							margin-right: 24rpx;
							margin-top: 10rpx;
						}

						.mony {
							text-align: right;
							font-size: 26rpx;
							color: #333;
							margin-right: 24rpx;
							margin-top: 5rpx;
						}
					}
				}

				.allMony {
					height: 72rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					// border-bottom: 2rpx solid #EFF0F2;

					.left {
						color: #333333;
						font-size: 26rpx;
					}

					.right {
						color: #F21515;
						font-size: 26rpx
					}

					.right_pay {
						color: #333;
						font-size: 26rpx
					}
				}
			}
		}

		.payment {
			margin-top: 8rpx;
			border-bottom: 10rpx solid #EFF0F2;

			.all {
				width: 690rpx;
				margin: 0 auto;
				height: 82rpx;
				font-size: 26rpx;
				color: #333333;

				.allMony_icon {
					width: 29rpx;
					height: 25rpx;
					margin-right: 15rpx;
				}

				.chose_icon {
					width: 32rpx;
					height: 32rpx;
					background-repeat: no-repeat;
					background-size: 100% 100%;
					background-image: url('../../../static/icon/chose_icon.png');
				}

				.actPay {
					background-image: url('../../../static/icon/chose_icon_select.png') !important;

				}
			}
		}

		.integral {
			width: 690rpx;
			margin: 10rpx auto;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 26rpx;
			color: #294D7C;
		}

		.ye_money {
			height: 80rpx;
			width: 500rpx;
			max-width: 600rpx;
			position: absolute;
			left: 28rpx;
			bottom: 0;
			right: 0;
			text-align: left;
			line-height: 80rpx;
			font-size: 26rpx;
		}

		.dw {
			border-top: 1rpx solid #E3E3EF;
			width: 750rpx;
			height: 80rpx;
			position: absolute;
			left: 0;
			bottom: 0;
			right: 0;
			text-align: right;
			line-height: 80rpx;

			.mony {
				color: #666666;
				font-size: 24rpx;

				text {
					font-size: 30rpx;
					color: #F21515;
				}
			}

			.zhifu {
				margin-left: 30rpx;
				display: inline-block;
				width: 220rpx;
				text-align: center;
				background: #4b968d;
				color: #fff;
			}
		}
	}

	.popup_container {
		width: 580rpx;
		height: 614rpx;

		.popup_top {
			width: 580rpx;
			height: 550rpx;
			border-radius: 25rpx;

			.popup_top_name {
				padding-top: 59rpx;
				font-size: 50rpx;
				color: #333333;
				text-align: center;
			}

			.popup_top_info {
				width: 520rpx;

				.popup_top_info_contant {
					padding: 23rpx 0;
					font-size: 28rpx;

					.popup_top_info_contant_ls {
						color: #B4B4B4;
					}

					.popup_top_info_contant_rg {
						color: #333;
					}
				}
			}

			.popup_button {
				width: 294rpx;
				height: 75rpx;
				background-image: url(../../../static/icon/btn.png);
				background-repeat: no-repeat;
				background-size: 100% 100%;
				text-align: center;
				color: #fff;
				font-size: 30rpx;
				line-height: 70rpx;
				margin-top: 55rpx;
			}
		}

		.popup_close {
			width: 44rpx;
			height: 44rpx;
			display: block;
			margin-top: 38rpx;
		}
	}
</style>
